package com.carmanage.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.carmanage.domain.Employees;
import com.carmanage.service.EmployeesService;
import com.carmanage.util.PageController;

@Controller
public class EmployeesController {

	private EmployeesService employeesService;

	public EmployeesService getEmployeesService() {
		return employeesService;
	}

	@Resource
	public void setEmployeesService(EmployeesService employeesService) {
		this.employeesService = employeesService;
	}

	@RequestMapping(value = "/saveEmp.do")
	public String save(@RequestParam("empID") int empID,
			@RequestParam("empName") String empName,
			@RequestParam("empDept") String empDept,
			@RequestParam("empGroup") int empGroup,
			@RequestParam("empPoint") String empPoint,
			@RequestParam("empClasses") int empClasses, HttpSession session) {
		Employees employee = new Employees(empID, empName, empDept, empGroup,
				empPoint, empClasses);
		employeesService.save(employee);
		PageController pc = (PageController) session.getAttribute("pc");
		int totalRows = employeesService.getTotalRows();
		int currPage = pc.getCurrentPage();
		PageController currPc = new PageController(totalRows, currPage);
		List<Employees> employees = employeesService.showAllEmps(currPc);
		session.setAttribute("pc", currPc);
		session.setAttribute("emps", employees);
		getDepts(session);
		return "pages/staffManage.jsp";
	}

	@RequestMapping(value = "/showEmps.do")
	public String showAllEmps(HttpSession session) {
		int totalRow = employeesService.getTotalRows();
		PageController pc = new PageController(totalRow, 1);
		List<Employees> employees = employeesService.showAllEmps(pc);
		session.setAttribute("emps", employees);
		session.setAttribute("pc", pc);
		getDepts(session);
		return "pages/staffManage.jsp";

	}

	@RequestMapping(value = "/changePages.do")
	public String changePages(@RequestParam("currentPage") int currentPage,
			HttpSession session) {
		PageController pc = (PageController) session.getAttribute("pc");
		pc.setCurrentPage(currentPage);
		List<Employees> employees = employeesService.showAllEmps(pc);
		session.setAttribute("emps", employees);
		return "pages/staffManage.jsp";

	}

	@RequestMapping(value = "/deleteEmp.do")
	public String deleteEmp(@RequestParam("Id") int id, HttpSession session) {
		employeesService.deleteEmpById(id);
		PageController pc = (PageController) session.getAttribute("pc");
		int totalRows = employeesService.getTotalRows();
		int currPage = pc.getCurrentPage();
		PageController currPc = new PageController(totalRows, currPage);
		List<Employees> employees = employeesService.showAllEmps(currPc);
		session.setAttribute("emps", employees);
		session.setAttribute("pc", currPc);
		getDepts(session);
		return "pages/staffManage.jsp";

	}

	@RequestMapping(value = "/groupSearch.do")
	public String groupSearch(@RequestParam("searchName") String searchName,
			@RequestParam("searchEmpId") int searchEmpId,
			@RequestParam("searchDept") String searchDept,
			@RequestParam("searchClasses") int searchClasses,
			@RequestParam("searchStation") String searchStation,
			HttpSession session) {
		Employees employee = new Employees(searchName, searchEmpId, searchDept,
				searchClasses, searchStation);
		int totalRow = employeesService.getGroupTotalRows(employee);
		PageController pc = new PageController(totalRow, 1);
		List<Employees> employees = employeesService.groupSearch(employee, pc);
		session.setAttribute("emps", employees);
		session.setAttribute("pc", pc);
		return "pages/staffManage.jsp";
	}

	public void getDepts(HttpSession session) {
		List<Object[]> depts = employeesService.getDepts();
		List<Object[]> classes = employeesService.getClasses();
		List<Object[]> stations = employeesService.getStations();
		session.setAttribute("depts", depts);
		session.setAttribute("classes", classes);
		session.setAttribute("stations", stations);
	}

	@RequestMapping(value = "/showModifyData.do")
	public @ResponseBody
	Employees showModifyData(@RequestParam("modifyEmpId") int modifyEmpId,
			HttpSession session) {
		Employees modifyEmps = employeesService.showModifyData(modifyEmpId);
		// session.setAttribute("modifyEmps", modifyEmps);
		System.out.println(modifyEmps);
		return modifyEmps;
	}
	
	 @RequestMapping(value="/modiEmp.do")
     public String modiEmp(@RequestParam("modiID") int modiID,@RequestParam("modiName")String modiName,@RequestParam("modiEmpId")int modiEmpId,@RequestParam("modiDept")String modiDept,
			   @RequestParam("modiGroup")int modiGroup,@RequestParam("modiStation")String modiStation,@RequestParam("modiClass")int modiClass,HttpSession session){
     	Employees employee=new Employees(modiEmpId,modiName,modiDept,modiGroup,modiStation,modiClass);
     	employeesService.modiEmp(modiID, employee);
     	PageController pc=(PageController) session.getAttribute("pc");
         int totalRows=employeesService.getTotalRows();
         int currPage=pc.getCurrentPage();
         PageController currPc=new PageController(totalRows,currPage);
     	List<Employees> employees=employeesService.showAllEmps(currPc);
     	session.setAttribute("pc", currPc);
     	session.setAttribute("emps", employees);
     	getDepts(session);
     	 return "pages/staffManage.jsp";
     }
}
